package com.yuan.zs.ui.account;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.yuan.base.framework.BaseFrameworkActivity;
import com.yuan.base.framework.BaseViewHolderAdapter;
import com.yuan.base.framework.bean.TipResult;
import com.yuan.base.framework.http.BaseHttpCallBack;
import com.yuan.base.framework.http.BaseHttpRequest;
import com.yuan.base.utils.DialogUtil;
import com.yuan.base.widget.AutoLoadList;
import com.yuan.zs.R;
import com.yuan.zs.Urls;

import java.util.HashMap;
import java.util.List;

/**
 * 账号管理列表页
 */
public class AccountManageSearchActivity extends BaseFrameworkActivity {

	// --------------------------控件--------------------------
	private AutoLoadList auto_load_list;
	private ListView data_list;// 下拉刷新
	// --------------------------变量--------------------------
	private List<JSONObject> dataList;
	private WaitAdapter waitAdapter;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_account_manage_search;
	}

	@Override
	protected void bindViews() {
		auto_load_list = findViewById(R.id.auto_load_list);
		auto_load_list.allowLoadMore(false);
		data_list = auto_load_list.getData_list();
		auto_load_list.setOnRefreshingListener(new AutoLoadList.OnRefreshingListener() {
			@Override
			public void onRefresh() {
				getListData();
			}

			@Override
			public void onLoadMore() {
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		auto_load_list.startRefreshing();
	}

	private void getListData() {
		HashMap<String, Object> params = new HashMap<>();
		BaseHttpRequest.get(Urls.getUrl(Urls.searchChildAccount), params, new BaseHttpCallBack<List<JSONObject>>() {

			@Override
			public void onFailure(int code, String msg) {
				super.onFailure(code, msg);
				auto_load_list.completeRefresh();
			}

			@Override
			public void onPost() {
				auto_load_list.completeRefresh();
			}

			@Override
			public void onSuccess(List<JSONObject> result) {
				dataList = result;
				init(true);
			}

		});
	}

	private void init(boolean needNew) {
		if (needNew) {
			waitAdapter = new WaitAdapter(this);
			waitAdapter.setData(dataList);
			data_list.setAdapter(waitAdapter);
		} else {
			waitAdapter.setData(dataList);
			waitAdapter.notifyDataSetChanged();
		}
	}

	private class WaitAdapter extends BaseViewHolderAdapter<JSONObject, Holder> {

		private LayoutInflater inflater;

		private WaitAdapter(Context context) {
			super(context);
		}

		@Override
		protected void bindViews(View convertView, Holder holder, JSONObject item) {
			holder.tv_account_name = convertView.findViewById(R.id.tv_account_name);
			holder.tv_account_phone = convertView.findViewById(R.id.tv_account_phone);
			holder.btn_delete = convertView.findViewById(R.id.btn_delete);
		}

		@Override
		protected View getConvertView(JSONObject item, LayoutInflater inflater, int position, ViewGroup parent) {
			this.inflater = inflater;
			return inflater.inflate(R.layout.item_account_list, parent, false);
		}

		@Override
		protected Holder getHolder() {
			return new Holder();
		}

		@Override
		protected void refreshView(int position, JSONObject item, View convertView, Holder holder) {
			holder.tv_account_name.setText(item.getString("name"));
			holder.tv_account_phone.setText(item.getString("phone"));
			holder.btn_delete.setOnClickListener(v -> {
				DialogUtil.confirm(AccountManageSearchActivity.this, "提示", "确认删除吗？", "确定", (dialog, which) -> {
					doDelete(item.getString("id"));
				}, "取消", null);

			});
		}
	}

	private void doDelete(String id) {
		String url = Urls.getUrl(Urls.removeChildAccount);
		HashMap<String, Object> params = new HashMap<>();
		params.put("id", id);
		BaseHttpRequest.post(url, params, new BaseHttpCallBack<TipResult>() {

			@Override
			public void onSuccess(TipResult result) {
				if (result.isSuccess()) {
					showToast("提交成功");
					auto_load_list.startRefreshing();
				} else {
					showToast(result.getMessage());
					auto_load_list.startRefreshing();
				}
			}
		});
	}

	private class Holder {
		TextView tv_account_name;
		TextView tv_account_phone;
		Button btn_delete;
	}
}
